package cn.eonshine.cuba.inf.modular.basic.service.impl;

import java.util.Date;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.basic.entity.Ecc;
import cn.eonshine.cuba.inf.modular.basic.entity.Mapping;
import cn.eonshine.cuba.inf.modular.basic.mapper.EccMapper;
import cn.eonshine.cuba.inf.modular.basic.service.IEccService;
import cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService;
import lombok.extern.log4j.Log4j;

/**
 * <p>
 * ECC 公司表 服务实现类
 * </p>
 *
 * @author Alan
 * @since 2018-07-28
 */
@Service
@Log4j
public class EccServiceImpl extends AbstractCommonService<EccMapper, Ecc> implements IEccService {
	
	/**   
	 * <p>Title: customerQueryWrapper</p>   
	 * <p>Description: </p>   
	 * @see cn.eonshine.cuba.inf.modular.common.service.AbstractCommonService#customerQueryWrapper()   
	 * @author: Alan
	 * @date:   2019-03-25 21:28:27
	 */
	@Override
	public QueryWrapper<Ecc> customerQueryWrapper() {
		QueryWrapper<Ecc> entity = new QueryWrapper<Ecc>();
		entity.eq("lsms_update", InfStatus.LSMS_UPDATE_EXTRACT_NO);
		entity.orderByAsc("CODE");
		return entity;
	}

	@Override
	public boolean isUseCutomerSelectSQL() {
		return false;
	}


	@Override
	public int transportDataHandle(Ecc ecc, int count) throws Exception {
		String code = ecc.getCode();
		Date dataInputDatetime = ecc.getDataInputDatetime();

		QueryWrapper<Ecc> entity = new QueryWrapper<Ecc>();
		//entity.where("code={0} and data_input_datetime={1}", code, dataInputDatetime);
		entity.lambda().eq(Ecc::getCode, code);
		entity.lambda().eq(Ecc::getDataInputDatetime, dataInputDatetime);
		
		boolean isDelete=false;
		if(this.isUpdate==0){
			isDelete = remove(entity);
		}else {
			isDelete = update( ecc,entity);
		}
		if (isDelete) {
			count++;
		}
		return count;
	}

	@Override
	public void mappingHandle(Ecc ecc) throws Exception {
		Mapping map = new Mapping();
		QueryWrapper<Mapping> entitys = new QueryWrapper<Mapping>();
		map.setLsmsId(ecc.getId());
		map.setDmz1(ecc.getCode());

		map.setDataType(InfStatus.DMZ_MAPPING_ECC);
		entitys.eq("lsms_id", ecc.getId());
		Mapping maps = mappingService.getOne(entitys);
		if (maps == null) {
			mappingService.save(map);
		}else {
			mappingService.update(map, entitys);
		}
	}


}
